import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
//引入svg插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
// https://vitejs.dev/config/
//导入mock插件
import { viteMockServe } from 'vite-plugin-mock'
export default defineConfig(({ command, mode }) => {
  // 获取各中环境下对应的变量
  let env = loadEnv(mode, process.cwd());
  return {
    plugins: [vue(),
    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
      symbolId: 'icon-[dir]-[name]',
    }),
    viteMockServe({
      enable: command === 'serve'
    })
    ],
    resolve: {
      //相对路径别名配置
      alias: {
        "@": path.resolve("./src")
      }
    },
    //scss全局变量配置
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/style/variable.scss";',
        }
      }
    },
    //代理跨域
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          //获取数据服务器地址
          target: env.VITE_SERVE,
          changeOrigin: true,
          //路径重写（真实路径若有api,则不需要路径重写）
          rewrite: (path) => path.replace(/^\/api/, ''),
        }
      }
    }
  }
})

//代理跨域问题解决
//https://vitejs.dev/config/